package cc.laop.controller;

import cc.laop.business.system.ResourceBusiness;
import cc.laop.business.system.RoleBusiness;
import cc.laop.constants.UserConstants;
import cc.laop.entity.system.Resource;
import cc.laop.entity.system.Role;
import cc.laop.entity.system.User;
import cc.laop.utils.ShiroSessionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("admin")
public class AdminController {

	@Autowired
	private ResourceBusiness resourceBusiness;

	@Autowired
	private RoleBusiness roleBusiness;

	@RequestMapping("index.do")
	@ResponseBody
	public Map<String, Object> index() {
		User user = (User) ShiroSessionUtils
				.getAttribute(UserConstants.CURRENT_USER);
		Map<String, Object> map = new HashMap<String, Object>();
		if (user != null) {
			List<Role> rolelist = roleBusiness.selectByUser(user.getId());
			List<Resource> reslist = new ArrayList<Resource>();
			if (rolelist != null) {
				for (Role role : rolelist) {
					reslist.addAll(role.getResourceList());
				}
			}
			reslist = resourceBusiness.toTree(reslist);
			map.put("resourceList", reslist);
			map.put("currentUser", user);
		}
		return map;
	}
}
